package com.tongzhou.tongzhou.quot.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.initInfo.model.Region;
import com.tongzhou.tongzhou.initInfo.model.SearchRegionCondition;
import com.tongzhou.tongzhou.quot.dao.IRegionDAO;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.PaperType;
import com.tongzhou.util.Util;

public class RegionDAO implements IRegionDAO{

	@Override
	public int addRegion(Region region) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into region(region_name,url) ");
		sql.append("values('"+Util.changeToObj(region.getRegion_name())+"',");
		sql.append("'"+region.getUrl()+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deleteRegion(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from region where id='"+id+"'");
		DBUtil.update(sql.toString());
	}

	@Override
	public Region getRegionById(int id) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select  id,region_name,url ");
		sql.append(" from region ");
		sql.append(" where id='"+id+"'");
		Region region=DBUtil.query(sql.toString(), new BeanHandler<Region>(Region.class));
		return region;
	}

	@Override
	public List<Region> listRegion(SearchRegionCondition condition)
			throws DBException {
		List<Region> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from region ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,region_name,url ");
		sql.append(" from region ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<Region>(Region.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updateRegion(Region region) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update Region set ");
		sql.append("url='"+Util.changeToObj(region.getUrl())+"' ");
		sql.append("where id='"+region.getId()+"'");
		DBUtil.update(sql.toString());
		
		sql.delete(0, sql.length());
		sql.append("delete from paper where paper_type_id in (select id from paper_type where region_id='"+region.getId()+"')");
		DBUtil.update(sql.toString());
		
		sql.delete(0, sql.length());
		sql.append("delete from paper_type where region_id='"+region.getId()+"'");
		DBUtil.update(sql.toString());
	}
	
	public List<Region> getRegionList() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,region_name from region ");
		List<Region> result=DBUtil.query(sql.toString(),new BeanListHandler<Region>(Region.class));
		return result;
	}

	@Override
	public List<Paper> getPaperList(String paperTypeId) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,paper_name,price,keng,paper_type_id from paper ");
		sql.append(" where 1=1 ");
		sql.append(" and paper_type_id='"+paperTypeId+"'");
		List<Paper> result=DBUtil.query(sql.toString(),new BeanListHandler<Paper>(Paper.class));
		return result;
	}

	@Override
	public List<PaperType> getPaperTypeList(String regionId) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,paper_type_name,region_id from paper_type ");
		sql.append(" where 1=1 ");
		sql.append(" and region_id='"+regionId+"'");
		List<PaperType> result=DBUtil.query(sql.toString(),new BeanListHandler<PaperType>(PaperType.class));
		return result;
	}

}
